﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.IO;

namespace SimpleText.Framework
{

public class Importer
{
	public TextWriter DebugWriter { get; set; }
	public bool RunInTestMode { get; set; }

	public Importer()
	{
	}

	public void DoImport(LanguageVersioningModel model, DataTable importDataTable)
	{
		//detect which of the languages are present in the input
		List<Language> languagesInImport = new List<Language>();
		foreach (Language lang in model.Languages)
		{
			foreach (DataColumn col in importDataTable.Columns)
			{
				if (lang.ShortName == col.ColumnName)
				{
					languagesInImport.Add(lang);
				}
			}
		}

		//import
		string textDefName = null;
		TextDefinition existingTextDef = null;
		foreach (DataRow row in importDataTable.Rows)
		{
			textDefName = Convert.ToString(row["name"]);
			if (textDefName == "dummy_do_not_translate_edit_or_delete")
			{
				//this is a dummy row used to ensure that OLE-DB imports does not truncate texts, skip it.
				continue;
			}

			if (model.TextDefinitions.ContainsKey(textDefName))
			{
				existingTextDef = model.TextDefinitions[textDefName];
			}
			else
			{
				existingTextDef = null;
			}

			if (existingTextDef == null)
			{
				//new text definition
				TextDefinition newTextDef = new TextDefinition();
				newTextDef.Name = textDefName;
				WriteDebugLine("Adding: " + textDefName + "");
				foreach (Language lang in languagesInImport)
				{
					WriteDebugLine("+ " + lang.ShortName + ": " + GetSubstring(Convert.ToString(row[lang.ShortName]), 30) + "");
					Text newText = new Text();
					newText.LanguageShortName = lang.ShortName;
					newText.Content = Convert.ToString(row[lang.ShortName]);
					newTextDef[lang.ShortName] = newText;
				}
				WriteDebugLine("");
				if (!RunInTestMode)
				{
					model.TextDefinitions[textDefName] = newTextDef;
				}
			}
			else
			{
				//update existing definition (only write to debug channel if any texts are actually created
				string inputContent = null;
				string textChangesDebug = "";
				foreach (Language lang in languagesInImport)
				{
					inputContent = Convert.ToString(row[lang.ShortName]);

					if (existingTextDef.Texts.ContainsKey(lang.ShortName))
					{
						//update existing text
						if (!String.IsNullOrEmpty(inputContent))
						{
							textChangesDebug += "- " + lang.ShortName + ": " + GetSubstring(Convert.ToString(row[lang.ShortName]), 30) + "\r\n";
							if (!RunInTestMode)
							{
								existingTextDef[lang.ShortName].Content = Convert.ToString(row[lang.ShortName]);
							}
						}
					}
					else
					{
						//create new text
						if (!String.IsNullOrEmpty(inputContent))
						{
							textChangesDebug += "+ " + lang.ShortName + ": " + GetSubstring(Convert.ToString(row[lang.ShortName]), 30) + "\r\n";
							Text newText = new Text();
							newText.LanguageShortName = lang.ShortName;
							newText.Content = Convert.ToString(row[lang.ShortName]);
							if (!RunInTestMode)
							{
								existingTextDef[lang.ShortName] = newText;
							}
						}
					}
				}
				if (textChangesDebug != "")
				{
					WriteDebugLine("Updating: " + textDefName + "");
					WriteDebugLine(textChangesDebug);
				}
			}

		}
	}

	protected string GetSubstring(string input, int length)
	{
		if (input.Length <= length)
		{
			return input;
		}
		else
		{
			return input.Substring(0, length) + "...";
		}
	}

	protected void WriteDebugLine(string text)
	{
		WriteDebug(text + "\r\n");
	}
	protected void WriteDebug(string text)
	{
		if (DebugWriter == null)
		{
			Console.Write(text);
		}
		else
		{
			if (DebugWriter != Console.Out)
			{
				Console.Write(text);
			}
			DebugWriter.Write(text);
		}
	}

}

}